package com.roger.myHome.controller;

import com.roger.myHome.aspect.WebLog;
import com.roger.myHome.entity.UseMedicineQryReq;
import com.roger.myHome.entity.dto.UserMedicineAddReq;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.UseMedicineService;
import com.roger.myHome.util.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/useMedicine")
public class UseMedicineController {
    @Resource
    private UseMedicineService useMedicineService;

    @Resource
    private UserHelper userHelper;

    @RequestMapping("/page")
    @ResponseBody
    public EUDataGridResult page(@RequestParam(value="page",required=false) String page,
                                 @RequestParam(value="rows",required=false) String rows, HttpServletRequest req){

        UseMedicineQryReq qryReq = new UseMedicineQryReq();
        qryReq.setPage((Integer.parseInt(page) - 1) * Integer.parseInt(rows));
        qryReq.setRows(Integer.parseInt(rows));

        String useTime = req.getParameter("useTime");
        String timePeriod = req.getParameter("timePeriod");

        if (!StringUtils.isNull(useTime)) {
            qryReq.setUseTime(useTime);
        }

        if (!StringUtils.isNull(timePeriod)) {
            qryReq.setTimePeriod(timePeriod);
        }

        return useMedicineService.page(qryReq);
    }

    @RequestMapping("/findByBloodSugarId")
    @ResponseBody
    public EUDataGridResult findByBloodSugarId(HttpServletRequest req){
        return useMedicineService.findByBloodSugarId(Long.valueOf(req.getParameter("bloodSugarId")));
    }

    @PostMapping("/add")
    @ResponseBody
    @WebLog
    public String add(HttpServletRequest request,UserMedicineAddReq req){
        req.setCreateUser(userHelper.getLoginUser(request).getId());
        req.setUseTime(req.getUseTime().substring(0,16));
        //System.out.println(req.getUseTime().substring(0,16));
        req.setWeek(DateUtil.dateToWeek(req.getUseTime().substring(0,10)));
        return useMedicineService.add(req);
    }

    @PostMapping("/addBatch")
    public String addBatch(UserMedicineAddReq req){
        return null;
    }

    @GetMapping("/deleteById/{id}")
    @ResponseBody
    public String deleteById(@PathVariable Long id){
        return useMedicineService.deleteById(id);
    }

    @GetMapping("/getPrompt/{keywords}")
    @ResponseBody
    public String getPrompt(@PathVariable String keywords){
        System.err.println(keywords);
        Map hp = new HashMap();
        hp.put("suggestions","4个单位优泌乐");
        return StringUtils.getJSONString(hp);
    }

}
